import 'package:f_enjoy_plus/components/Header.dart';
import 'package:f_enjoy_plus/components/HouseTatusTag.dart';
import 'package:f_enjoy_plus/core/constants/app_colors.dart';
import 'package:f_enjoy_plus/components/ImagePreview.dart';
import 'package:f_enjoy_plus/pages/house/addHouse.dart';
import 'package:f_enjoy_plus/utils/http.dart';
import 'package:flutter/material.dart';

class HouseDetail extends StatefulWidget {
  const HouseDetail({super.key, required this.routerParams});
  final Map routerParams;

  @override
  State<HouseDetail> createState() => _HouseDetailState();
}

class _HouseDetailState extends State<HouseDetail> {
  Map houseInfo = {
    "id": "string",
    "point": "string",
    "building": "string",
    "room": "string",
    "name": "string",
    "gender": 1,
    "mobile": "string",
    "idcardFrontUrl":
        "https://img1.baidu.com/it/u=3870046931,1670196107&fm=253&fmt=auto&app=120&f=JPEG?w=261&h=174",
    "idcardBackUrl":
        "https://img1.baidu.com/it/u=3870046931,1670196107&fm=253&fmt=auto&app=120&f=JPEG?w=261&h=174",
    "status": 1,
  };
  @override
  void initState() {
    super.initState();
    print(widget.routerParams);
    if (widget.routerParams['id'] != null) {
      getHouseInfo();
    }
  }

  getHouseInfo() async {
    try {
      final res = await http.get('/room/${widget.routerParams['id']}');
      setState(() {
        houseInfo = res;
      });
    } catch (e) {
      print(e);
    }
  }

  deleteDialog() {
    showDialog(
      context: context,
      builder:
          (context) => AlertDialog(
            title: Text('删除房屋'),
            content: Text('确定删除房屋吗？'),
            actions: [
              TextButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: Text('取消'),
              ),
              TextButton(
                onPressed: () {
                  Navigator.of(context).pop();
                  deleteHouse();
                },
                child: Text('确定'),
              ),
            ],
          ),
    );
  }

  deleteHouse() async {
    try {
      await http.delete('/room/${widget.routerParams['id']}');
      Navigator.of(context).pop();
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Header(title: '房屋详情'),
      body: Container(
        child: Column(
          children: [
            Expanded(
              child: ListView(
                children: [
                  Container(
                    padding: EdgeInsets.all(16),
                    child: Text(
                      '房屋信息',
                      style: TextStyle(
                        fontSize: 12,
                        color: AppColors.textSecondary,
                      ),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.all(16),
                    color: AppColors.white,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(houseInfo['point'] ?? ''),
                        HouseStatusTag(status: houseInfo['status'] ?? 0),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.all(16),
                    child: Text(
                      '业主信息',
                      style: TextStyle(
                        fontSize: 12,
                        color: AppColors.textSecondary,
                      ),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.all(16),
                    color: AppColors.white,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        SizedBox(width: 80, child: Text('房间号')),
                        Text('${houseInfo['building']} - ${houseInfo['room']}'),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.all(16),
                    color: AppColors.white,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        SizedBox(width: 80, child: Text('业主')),
                        Text(houseInfo['name'] ?? ''),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.all(16),
                    color: AppColors.white,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        SizedBox(width: 80, child: Text('手机号')),
                        Text(houseInfo['mobile'] ?? ''),
                      ],
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(16),
                    child: Text(
                      '本人身份证',
                      style: TextStyle(
                        fontSize: 12,
                        color: AppColors.textSecondary,
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
                      Navigator.of(context).push(
                        MaterialPageRoute(
                          builder:
                              (context) => ImagePreview(
                                imageUrl: houseInfo['idcardFrontUrl'],
                                title: '身份证正面',
                              ),
                        ),
                      );
                    },
                    child: Container(
                      padding: EdgeInsets.all(16),
                      margin: EdgeInsets.symmetric(horizontal: 16),
                      decoration: BoxDecoration(
                        color: AppColors.white,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child:
                          houseInfo['idcardFrontUrl'] != null
                              ? Image.network(houseInfo['idcardFrontUrl'])
                              : Text('暂无身份证'),
                    ),
                  ),
                  SizedBox(height: 16),
                  GestureDetector(
                    onTap: () {
                      Navigator.of(context).push(
                        MaterialPageRoute(
                          builder:
                              (context) => ImagePreview(
                                imageUrl: houseInfo['idcardBackUrl'],
                                title: '身份证背面',
                              ),
                        ),
                      );
                    },
                    child: Container(
                      padding: EdgeInsets.all(16),
                      margin: EdgeInsets.symmetric(horizontal: 16),
                      decoration: BoxDecoration(
                        color: AppColors.white,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child:
                          houseInfo['idcardBackUrl'] != null
                              ? Image.network(houseInfo['idcardBackUrl'])
                              : Text('暂无身份证'),
                    ),
                  ),
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.all(16),
              child: Row(
                children: [
                  Expanded(
                    child: ElevatedButton(
                      onPressed: () {
                        Navigator.of(context).push(
                          MaterialPageRoute(
                            builder:
                                (context) => AddHouse(
                                  routerParams: {'id': houseInfo['id']},
                                ),
                          ),
                        );
                      },
                      child: Text('编辑房屋'),
                      style: ElevatedButton.styleFrom(
                        backgroundColor: AppColors.primary,
                        foregroundColor: AppColors.white,
                        minimumSize: Size(double.infinity, 48),
                      ),
                    ),
                  ),
                  SizedBox(width: 16), // 间距
                  Expanded(
                    child: ElevatedButton(
                      onPressed: deleteDialog,
                      child: Text('删除房屋'),
                      style: ElevatedButton.styleFrom(
                        backgroundColor: AppColors.error,
                        foregroundColor: AppColors.white,
                        minimumSize: Size(double.infinity, 48),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(height: 16),
          ],
        ),
      ),
    );
  }
}
